"""

"""

from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.metrics import dp
from ..components.header import AppHeader
from ..components import theme

class MainWindow(Screen):
    """"""
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._build_ui()
    
    def _build_ui(self):
        """主界面：深色背景 + 居中卡片布局 + 统一主题样式"""
        # 背景容器
        root = BoxLayout(orientation='vertical', spacing=theme.SPACING, padding=theme.PADDING)

        # 深色背景
        with root.canvas.before:
            from kivy.graphics import Color, Rectangle
            Color(*theme.BG)
            self._bg_rect = Rectangle()

        def _update_rect(instance, value):
            self._bg_rect.pos = instance.pos
            self._bg_rect.size = instance.size

        root.bind(pos=_update_rect, size=_update_rect)

        # 居中卡片
        center = AnchorLayout(anchor_x='center', anchor_y='center')

        content_card = BoxLayout(orientation='vertical', spacing=theme.SPACING, padding=theme.PADDING,
                                 size_hint=(None, None), width=dp(900), height=dp(580))

        with content_card.canvas.before:
            from kivy.graphics import Color, Rectangle
            Color(*theme.CARD_BG)
            self._card_rect = Rectangle()
        
        def _update_card_rect(instance, value):
            self._card_rect.pos = instance.pos
            self._card_rect.size = instance.size
        content_card.bind(pos=_update_card_rect, size=_update_card_rect)

        # 页头
        header = AppHeader(
            title="Local Smart Video Summary",
            subtitle="Privacy Protection • Offline Work • Open Source",
        )
        content_card.add_widget(header)

        # 介绍文本
        intro_text = (
            "Key Features\n"
            "• Video speech transcription (OpenAI Whisper)\n"
            "• AI smart summarization (Ollama local LLM)\n"
            "• Keyword extraction and chapter division\n"
            "• Multiple output format support\n\n"
            "Privacy Protection\n"
            "• All processing done locally\n"
            "• No data uploaded to any server\n"
            "• Completely offline work\n\n"
            "Get Started: Click the button below to start processing your video files"
        )

        intro_label = Label(
            text=intro_text,
            font_size=theme.BODY + dp(8),
            color=theme.TEXT,
            halign='left',
            valign='top',
        )
        intro_label.bind(size=lambda l, v: setattr(l, 'text_size', (l.width, None)))
        content_card.add_widget(intro_label)

        # 按钮区
        button_layout = GridLayout(cols=2, rows=2, spacing=dp(16), size_hint_y=None, height=dp(160))

        start_btn = Button(text="Start Processing Video", font_size=dp(28))
        theme.style_button(start_btn, bg_color=theme.ACCENT)
        start_btn.bind(on_press=self._on_start_processing)
        button_layout.add_widget(start_btn)

        settings_btn = Button(text="Settings", font_size=dp(28))
        theme.style_button(settings_btn, bg_color=theme.MUTED_BG, text_color=theme.TEXT)
        settings_btn.bind(on_press=self._on_open_settings)
        button_layout.add_widget(settings_btn)

        help_btn = Button(text="Help", font_size=dp(28))
        theme.style_button(help_btn, bg_color=theme.MUTED_BG, text_color=theme.TEXT)
        help_btn.bind(on_press=self._on_show_help)
        button_layout.add_widget(help_btn)

        exit_btn = Button(text="Exit App", font_size=dp(28))
        theme.style_button(exit_btn, bg_color=theme.DANGER)
        exit_btn.bind(on_press=self._on_exit)
        button_layout.add_widget(exit_btn)

        content_card.add_widget(button_layout)

        center.add_widget(content_card)
        root.add_widget(center)

        self.add_widget(root)
    
    def _on_start_processing(self, instance):
        """"""
        app = App.get_running_app()
        app.navigate_to('upload')
    
    def _on_open_settings(self, instance):
        """"""
        app = App.get_running_app()
        app.navigate_to('settings')
    
    def _on_show_help(self, instance):
        """"""
        # TODO: 
        pass
    
    def _on_exit(self, instance):
        """"""
        app = App.get_running_app()
        app.stop()